package com.onedec.menu.service.impl;

import com.onedec.menu.dao.OnedecPermissionDao;
import com.onedec.menu.entity.OnedecPermission;
import com.onedec.menu.entity.OnedecPermissionExample;
import com.onedec.menu.service.MenuService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


@Service
public class MenuServiceImpl implements MenuService {

    @Resource
    private OnedecPermissionDao permissionDao;

    @Override
    public OnedecPermission selectById(int id) throws Exception {
        return permissionDao.selectByPrimaryKey(id);
    }

    @Override
    public List<OnedecPermission> getMenus(int eid) throws Exception {
        List<OnedecPermission> mainMenus = permissionDao.selectMainMenusById(eid);
        Map<String, Integer> map = new HashMap<>();
        System.out.println(mainMenus);
        for (OnedecPermission mainMenu : mainMenus) {
            System.out.println("一级菜单：" + mainMenu);
            map.put("eid", eid);
            map.put("parentId", mainMenu.getPermissionId());
            List<OnedecPermission> list = permissionDao.selectSubMenusById(map);
            mainMenu.setSubMenus(list);
            System.out.println("成功添加子菜单");
        }
        return mainMenus;
    }

    @Override
    public List<OnedecPermission> getAllSubMenus() throws Exception {
        OnedecPermissionExample onedecPermissionExample = new OnedecPermissionExample();
        OnedecPermissionExample.Criteria or = onedecPermissionExample.or();
        or.andPermissionLevelEqualTo((byte) 2);
        return permissionDao.selectByExample(onedecPermissionExample);
    }
}
